#ifndef _SINGLELRU_H_
#define _SINGLELRU_H_

#include "LRUcache.h"
#include <mutex>

class singleLRU{
public:
    static LRUcache* getInstance(){
        if(nullptr==_LRUcache){
            std::lock_guard<std::mutex> lock(_mutex);
            _LRUcache=new LRUcache();
            readLRU();
            atexit(destroy);
        }
        return _LRUcache;
    }

    static void destroy(){
        if(_LRUcache){
            // wirteLRU();
            delete _LRUcache;
            _LRUcache=nullptr;
        }
    }

    static void readLRU(){
        std::ifstream ifs("../../data/LRU.dat");
        if(!ifs){
            return;
        }
        std::string line;
        std::string word;
        std::string key;
        while(getline(ifs,line))
        {
            std::istringstream iss(line);
            iss>>key;
            std::vector<std::string> vec;
            while(iss>>word)
            {
                vec.push_back(word);
            }
            _LRUcache->LRUpush(key,vec);
        }
        ifs.close();
    }
    
    static void wirteLRU(){
        std::string filename("../../data/LRU.dat");
        _LRUcache->writeQue(filename);
    }

private:
    singleLRU() {}
    ~singleLRU() {}
    singleLRU(const singleLRU&)=delete;
    singleLRU operator=(const singleLRU&)=delete;
private:
    static LRUcache* _LRUcache;
    static std::mutex _mutex;
};
#endif
